/* Copyright 2014 predic8 GmbH, www.predic8.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. */
package com.predic8.membrane.annot.generator;

import java.io.BufferedWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.processing.FilerException;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.tools.FileObject;

import com.predic8.membrane.annot.AnnotUtils;
import com.predic8.membrane.annot.model.AttributeInfo;
import com.predic8.membrane.annot.model.ChildElementDeclarationInfo;
import com.predic8.membrane.annot.model.ChildElementInfo;
import com.predic8.membrane.annot.model.ElementInfo;
import com.predic8.membrane.annot.model.MainInfo;
import com.predic8.membrane.annot.model.Model;

public class BlueprintParsers {

	private final ProcessingEnvironment processingEnv;

	public BlueprintParsers(ProcessingEnvironment processingEnv) {
		this.processingEnv = processingEnv;
	}

	public void writeParserDefinitior(Model m) throws IOException {

		for (MainInfo main : m.getMains()) {
			List<Element> sources = new ArrayList<Element>();
			sources.addAll(main.getInterceptorElements());
			sources.add(main.getElement());

			try {
				FileObject o = processingEnv.getFiler().createSourceFile(
						main.getAnnotation().outputPackage() + ".blueprint" + ".BlueprintNamespaceParser",
						sources.toArray(new Element[0]));
				BufferedWriter bw = new BufferedWriter(o.openWriter());
				try {
					bw.write("/* Copyright 2014 predic8 GmbH, www.predic8.com\r\n" +
							"\r\n" +
							"   Licensed under the Apache License, Version 2.0 (the \"License\");\r\n" +
							"   you may not use this file except in compliance with the License.\r\n" +
							"   You may obtain a copy of the License at\r\n" +
							"\r\n" +
							"   http://www.apache.org/licenses/LICENSE-2.0\r\n" +
							"\r\n" +
							"   Unless required by applicable law or agreed to in writing, software\r\n" +
							"   distributed under the License is distributed on an \"AS IS\" BASIS,\r\n" +
							"   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n" +
							"   See the License for the specific language governing permissions and\r\n" +
							"   limitations under the License. */\r\n" +
							"\r\n" +
							"package " + main.getAnnotation().outputPackage() + ".blueprint;\r\n" +
							"\r\n" +
							"/**\r\n" +
							"  * Automatically generated by " + BlueprintParsers.class.getName() + ".\r\n" +
							"  */\r\n" +
							"public class BlueprintNamespaceParser extends com.predic8.membrane.annot.parser.BlueprintNamespaceParser {\r\n" +
							"\r\n" +
							"	public void init() {\r\n");
					for (ElementInfo i : main.getIis()) {
						if (i.getAnnotation().topLevel()) {
							bw.write("		registerGlobalBeanDefinitionParser(\"" + i.getAnnotation().name() + "\", new " + i.getParserClassSimpleName() + "());\r\n");
						} else {
							for (ChildElementDeclarationInfo cedi : i.getUsedBy()) {
								for (ChildElementInfo cei : cedi.getUsedBy()) {
									TypeElement element = cei.getEi().getElement();
									String clazz = AnnotUtils.getRuntimeClassName(element);
									bw.write("		registerLocalBeanDefinitionParser(\"" + clazz + "\", \"" + i.getAnnotation().name() + "\", new " + i.getParserClassSimpleName() + "());\r\n");
								}
							}
						}
					}
					bw.write(
							"	}\r\n" +
									"}\r\n" +
							"");
				} finally {
					bw.close();
				}
			} catch (FilerException e) {
				if (e.getMessage().contains("Source file already created"))
					return;
				throw e;
			}
		}
	}

	public void writeParsers(Model m) throws IOException {
		for (MainInfo main : m.getMains()) {
			for (ElementInfo ii : main.getIis()) {
				List<Element> sources = new ArrayList<Element>();
				sources.add(main.getElement());
				sources.add(ii.getElement());

				String interceptorClassName = ii.getElement().getQualifiedName().toString();

				try {
					FileObject o = processingEnv.getFiler().createSourceFile(main.getAnnotation().outputPackage() + ".blueprint" + "." + ii.getParserClassSimpleName(),
							sources.toArray(new Element[0]));
					BufferedWriter bw = new BufferedWriter(o.openWriter());
					try {
						bw.write("/* Copyright 2014 predic8 GmbH, www.predic8.com\r\n" +
								"\r\n" +
								"   Licensed under the Apache License, Version 2.0 (the \"License\");\r\n" +
								"   you may not use this file except in compliance with the License.\r\n" +
								"   You may obtain a copy of the License at\r\n" +
								"\r\n" +
								"   http://www.apache.org/licenses/LICENSE-2.0\r\n" +
								"\r\n" +
								"   Unless required by applicable law or agreed to in writing, software\r\n" +
								"   distributed under the License is distributed on an \"AS IS\" BASIS,\r\n" +
								"   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r\n" +
								"   See the License for the specific language governing permissions and\r\n" +
								"   limitations under the License. */\r\n" +
								"\r\n" +
								"package " + main.getAnnotation().outputPackage() + ".blueprint;\r\n" +
								"\r\n" +
								"import com.predic8.membrane.annot.parser.BlueprintElementParser;\r\n" +
								"import com.predic8.membrane.annot.parser.BlueprintParser;\r\n" +
								"import org.apache.aries.blueprint.ParserContext;\r\n" +
								"import org.osgi.service.blueprint.reflect.Metadata;\r\n" +
								"import org.apache.aries.blueprint.mutable.MutableBeanMetadata;\r\n" +
								"import org.osgi.service.blueprint.reflect.BeanMetadata;\r\n" +
								"import org.w3c.dom.Element;\r\n" +
								"");
						bw.write(
								"\r\n" +
										"/**\r\n" +
										"  * Automatically generated by " + BlueprintParsers.class.getName() + ".\r\n" +
										"  */\r\n" +
										"public class " + ii.getParserClassSimpleName() + " extends BlueprintElementParser {\r\n" +
										"	public Metadata parse(BlueprintParser globalParser, Element element, ParserContext context) {\r\n" +
										"		MutableBeanMetadata mcm = context.createMetadata(MutableBeanMetadata.class);\r\n" +
										"		mcm.setId(context.generateId());\r\n" +
										"		mcm.setScope(BeanMetadata.SCOPE_SINGLETON);\r\n" +
										"		mcm.setRuntimeClass(" + interceptorClassName + ".class);\r\n" +
										"		applySpringInterfacePatches(context, " + interceptorClassName + ".class, mcm);\r\n" +
								"\r\n");

						if (ii.isHasIdField())
							bw.write("		setPropertyIfSet(context, \"id\", element, mcm);\r\n");
						bw.write(
								"		setIdIfNeeded(element, context, \"" + ii.getAnnotation().name() + "\");\r\n");
						for (AttributeInfo ai : ii.getAis()) {
							if (ai.getXMLName().equals("id"))
								continue;
							if (ai.isBeanReference(processingEnv.getTypeUtils())) {
								if (!ai.isRequired())
									bw.write("		if (element.hasAttribute(\"" + ai.getXMLName() + "\"))\r\n");
								bw.write("		setPropertyReference(context, \"" + ai.getSpringName() + "\", element.getAttribute(\"" + ai.getXMLName() + "\"), mcm);\r\n");
							} else {
								bw.write("		setProperty" + (ai.isRequired() ? "" : "IfSet") + "(context, \"" + ai.getXMLName() + "\", \"" + ai.getSpringName() + "\", element, mcm" + (ai.isEnum(processingEnv.getTypeUtils()) ? ", true" : "") + ");\r\n");
							}
							if (ai.getXMLName().equals("name"))
								bw.write("		element.removeAttribute(\"name\");\r\n");
						}
						if (ii.getOai() != null) {
							bw.write("		setProperties(context, \"" + ii.getOai().getSpringName() + "\", element, mcm);\r\n");
						}
						if (ii.getTci() != null)
							bw.write("		setProperty(context, mcm, \"" + ii.getTci().getPropertyName() + "\", element.getTextContent());\r\n");
						else
							bw.write("		parseChildren(element, context, mcm, globalParser);\r\n");
						for (ChildElementInfo cei : ii.getCeis())
							if (cei.isList() && cei.isRequired()) {
								bw.write("		if (!isPropertySet(mcm, \"" + cei.getPropertyName() + "\"))\r\n");
								bw.write("			throw new RuntimeException(\"Property '" + cei.getPropertyName() + "' is required, but none was defined (empty list).\");\r\n");
							}

						bw.write(
								"" +
								"		return mcm;\r\n");
						bw.write(
								"	}\r\n");

						bw.write(
								"	@Override\r\n" +
								"	protected void handleChildObject(Element ele, ParserContext context, MutableBeanMetadata mcm, Class<?> clazz, Object child) {\r\n");
						for (ChildElementInfo cei : ii.getCeis()) {
							bw.write(
									"		if (" + cei.getTypeDeclaration().getQualifiedName() + ".class.isAssignableFrom(clazz)) {\r\n");
							if (cei.isList())
								bw.write(
										"			appendToListProperty(context, mcm, \"" + cei.getPropertyName() + "\"" + ", child);\r\n");
							else
								bw.write(
										"			setProperty(context, mcm, \"" + cei.getPropertyName() + "\"" + ", child);\r\n");
							bw.write(
									"		} else \r\n");
						}
						bw.write(
								"		{\r\n" +
										"			throw new RuntimeException(\"Unknown child class \\\"\" + clazz + \"\\\".\");\r\n" +
								"		}\r\n");
						bw.write(
								"	}\r\n");

						bw.write(
								"}\r\n" +
								"");
					} finally {
						bw.close();
					}
				} catch (FilerException e) {
					if (e.getMessage().contains("Source file already created"))
						return;
					throw e;
				}

			}
		}

	}


}
